package servlet_casa_test;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import servlet_utente_test.login;

public class ConfiguraCasaTest {

	
	@Before
	public void setUp() throws Exception {
		this.conf=new configuraCasa();
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public final void testDoPost() throws ServletException, IOException {
		HttpServletRequest request = mock(HttpServletRequest.class);       
        HttpServletResponse response = mock(HttpServletResponse.class); 

        when(request.getParameter("nome")).thenReturn("casa");
        when(request.getParameter("indirizzo")).thenReturn("via san barbato");
        when(request.getParameter("numero")).thenReturn("15");
        when(request.getParameter("mq")).thenReturn("80.4");
        when(request.getParameter("citta")).thenReturn("avellino");
        
        conf.doPost(request, response);
        bean.Casa c=conf.getCasa();
        assertEquals(c.getIndirizzo(), "via san barbato");
	}
	
	//test con l'inserimento dei metri quadri con il carattere ',' invece che con il '.'
	@Test

	public final void testDoPost2() throws ServletException, IOException {
		HttpServletRequest request = mock(HttpServletRequest.class);       
        HttpServletResponse response = mock(HttpServletResponse.class); 

        when(request.getParameter("nome")).thenReturn("casa");
        when(request.getParameter("indirizzo")).thenReturn("via san barbato");
        when(request.getParameter("numero")).thenReturn("15");
        when(request.getParameter("mq")).thenReturn("80,4");
        when(request.getParameter("citta")).thenReturn("avellino");
        
        conf.doPost(request, response);
        bean.Casa c=conf.getCasa();
        assertEquals(c.getMq(), 80.4, 0.001);
        
        
	}
	
	// test con l'inserimento di lettere invece di numeri nel campo numero civico 
	@Test()
	
	public final void testDoPost3() throws ServletException, IOException {
		HttpServletRequest request = mock(HttpServletRequest.class);       
        HttpServletResponse response = mock(HttpServletResponse.class); 

        when(request.getParameter("nome")).thenReturn("casa");
        when(request.getParameter("indirizzo")).thenReturn("via san barbato");
        when(request.getParameter("numero")).thenReturn("gggg");
        when(request.getParameter("mq")).thenReturn("80,4");
        when(request.getParameter("citta")).thenReturn("avellino");
        
        conf.doPost(request, response);
        bean.Casa c=conf.getCasa();
        assertNull(c);
        
	}
	
	// test con l'inserimento di lettere invece di numeri nel campo mq
		@Test()

		public final void testDoPost4() throws ServletException, IOException {
			HttpServletRequest request = mock(HttpServletRequest.class);       
	        HttpServletResponse response = mock(HttpServletResponse.class); 

	        when(request.getParameter("nome")).thenReturn("casa");
	        when(request.getParameter("indirizzo")).thenReturn("via san barbato");
	        when(request.getParameter("numero")).thenReturn("15");
	        when(request.getParameter("mq")).thenReturn("gggg");
	        when(request.getParameter("citta")).thenReturn("avellino");
	        
	        conf.doPost(request, response);
	        bean.Casa c=conf.getCasa();
	        assertNull(c);
	        
		}
	private configuraCasa conf;
}
